package com.xxlie.auth.security.auth.ajax;

import com.xxlie.auth.domain.User;
import com.xxlie.auth.security.auth.AjaxAuthenticationToken;
import com.xxlie.auth.security.model.UserContext;
import com.xxlie.auth.service.impl.UserServiceImpl;
import com.xxlie.auth.common.CommUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * AjaxAuthenticationProvider
 *
 * @author xxlie.com
 * @since 2017/7/30
 */
@Component
public class AjaxPhoneAuthenticationProvider implements AuthenticationProvider {
    private final PasswordEncoder encoder;
    private final UserServiceImpl userService;

    @Autowired
    CommUtil commUtil;

    @Autowired
    public AjaxPhoneAuthenticationProvider(final UserServiceImpl userService, final PasswordEncoder encoder) {
        this.userService = userService;
        this.encoder = encoder;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        Assert.notNull(authentication, "No authentication data provided");

        String phone = (String) authentication.getPrincipal();
        String smsCode = (String) authentication.getCredentials();

        Optional<User> users = userService.findByPhone(phone);
        User user = null;
        if(users.isPresent()) {
            user = users.get();
        } else {
            throw new InsufficientAuthenticationException("手机号未注册");
        }

        //验证验证码
        Boolean checkResult = commUtil.validateSmsCode(phone, smsCode);
        if (!checkResult) {
            throw new InsufficientAuthenticationException("验证码错误");
        }
        if (user.getRole() == null)
            throw new InsufficientAuthenticationException("User has no role assigned");

        List<GrantedAuthority> authorities = new ArrayList<>(1);
        authorities.add(new SimpleGrantedAuthority(user.getRole().authority()));

        UserContext userContext = UserContext.create(user.getUsername(), authorities);
        
        //设置扩展参数
        AjaxAuthenticationToken authenticationToken = (AjaxAuthenticationToken)authentication;
        Map<String, Object> extraParams = authenticationToken.getExtraParams();
        userContext.setExtraParams(extraParams);

        return new AjaxAuthenticationToken(userContext, null, userContext.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return (AjaxAuthenticationToken.class.isAssignableFrom(authentication));
    }
}
